<template>
<!-- f封装的收藏 -->
<!-- name="star-o" -->
  <div>
  <van-icon
        :color="value?'#3296fa': '#777'"
        :name="value?'star': 'star-o'"
     @click="oncollect"
     />
  </div>
</template>

<script>
import { addCollectArticle, deleteCollectArticle } from '@/api/article.js'
import { Toast } from 'vant'
export default {
  name: 'ArticleIndex',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Number, Object, String],
      required: true
    }
  },
  data () {
    return {

    }
  },

  created () {

  },

  methods: {
    async  oncollect () {
      // Toast.loading
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '加载中'
      })
      try {
        if (this.value) {
          console.log('已收藏')
          await deleteCollectArticle(this.artId + '')// artId是父组件文章id
        } else {
          console.log('未收藏')
          await addCollectArticle(this.artId + '')
        }
        this.$emit('input', !this.value)
        Toast.success('操作成功')
      } catch (error) {
        console.log(error)
        Toast.false('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.d{
font-size: 1;
}
</style>
